/*
 * Created on Jan 11, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.scohen.juploadr.ui.thumbnail;

import java.io.File;

import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Transform;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.scohen.juploadr.app.ImageAttributes;
import org.scohen.juploadr.ui.ReusableUIFactory;
import org.scohen.juploadr.ui.Transforms;
import org.scohen.juploadr.uploadapi.ImageUploadApi;
import org.scohen.juploadr.uploadapi.abilities.FileTypeNotSupportedException;

/**
 * @author steve
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class UploadThumbnail extends Canvas implements Thumbnail {

    private Image image;
    private ImageData originalData;

    private ImageAttributes attrs;
    private Color borderColor;
    private Color selectedBackground;
    private Color background;
    private int currentRotation = 0;

    /**
     * @param arg0
     * @param arg1
     */
    UploadThumbnail(Composite parent, ImageAttributes attrs, int style) {
        super(parent, style);
        this.attrs = attrs;

        ReusableUIFactory uiFactory = ReusableUIFactory.getInstance();
        borderColor = uiFactory.getThumbnailBorderColor();
        background = uiFactory.getThumbnailBackgroundColor();
        selectedBackground = uiFactory.getThumbnailSelectedBackgroundColor();
        thumbnailChanged();

        addDisposeListener(new DisposeListener() {
            public void widgetDisposed(DisposeEvent e) {
                image.dispose();
            }
        });
        addPaintListener(new PaintListener() {

            public void paintControl(PaintEvent e) {
                UploadThumbnail.this.paintControl(e);
            }

        });
    }

    public void thumbnailChanged() {
        currentRotation = 0;
        generateButtonImage(ReusableUIFactory.getInstance().getDisplay(), attrs.getThumbnail().getImageData());
        redraw();
    }

    private void generateButtonImage(Display display, ImageData thumbnailData) {
        Image thumbnail = null;;
        File f = new File(attrs.getImagePath());
        try {
            ThumbnailBuilder builder = ImageUploadApi.getInstance().getAbilities().getUploadAbilities().getThumbnailBuilder(f);
            thumbnail = builder.buildSquareThumbnail(attrs.getThumbnail(),new Point(THUMBNAIL_WIDTH,THUMBNAIL_HEIGHT));
        } catch (FileTypeNotSupportedException e) {
            e.printStackTrace();
        }
        
        if (image != null && !image.isDisposed()) {
            image.dispose();
            image = thumbnail;
        }        
        
//        float xScale = (float)thumbnailData.width / (float)THUMBNAIL_WIDTH;
//        float yScale = (float) thumbnailData.height / (float)THUMBNAIL_HEIGHT;
//        Transform tx=  new Transform(display);
//        System.out.printf("scale (%f,%f)\n",xScale,yScale);
//        tx.scale(xScale, yScale);
//        gfx.setTransform(tx);
//        gfx.drawImage(attrs.getThumbnail(), 0,0);
//        tx.dispose();
        
        image = new Image(display, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);
        GC gfx = new GC(image);
        
        int smallerSide = Math.min(thumbnailData.width, thumbnailData.height);
        gfx.drawImage(attrs.getThumbnail(), 0, 0, smallerSide, smallerSide, 0, 0, THUMBNAIL_WIDTH,
                THUMBNAIL_HEIGHT);
               

        gfx.dispose();
        originalData = image.getImageData();
        
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
     */
    void paintControl(PaintEvent event) {
        GC gc = event.gc;
        ImageData imageData = image.getImageData();
        int attRotation = attrs.getRotation();
        if (attRotation != currentRotation) {
            // rotate the image
            int rotation = attRotation;
            image.dispose();
            if (rotation != 0) {
                ImageData rotated = Transforms.rotate(originalData, rotation);
                image = new Image(event.display, rotated);
            } else {
                image = new Image(event.display, originalData);
            }
            GC imgGC = new GC(image);
            imgGC.drawImage(image, 0, 0);

            imgGC.dispose();
            currentRotation = attRotation;
        }
        gc.setForeground(borderColor);
        gc.setBackground((attrs.isSelected()) ? selectedBackground : background);
        gc.fillRectangle(1, 1, TOTAL_WIDTH, TOTAL_HEIGHT);
        gc.drawRectangle(0, 0, TOTAL_WIDTH - 1, TOTAL_HEIGHT - 1);
        gc.drawImage(image, BORDER_WIDTH, BORDER_WIDTH);
        gc.drawRectangle(BORDER_WIDTH, BORDER_WIDTH, imageData.width, imageData.height);

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.swt.widgets.Control#computeSize(int, int)
     */
    public Point computeSize(int x, int y) {
        return new Point(TOTAL_WIDTH, TOTAL_HEIGHT);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.swt.widgets.Control#computeSize(int, int, boolean)
     */
    public Point computeSize(int arg0, int arg1, boolean arg2) {
        return computeSize(arg0, arg1);
    }

    /**
     * @return Returns the image.
     */
    public Image getImage() {
        return image;
    }

    public ImageAttributes getImageAttributes() {
        return attrs;
    }
}